<?php
namespace Part02_StrategyMode\Code1;

/**
 * 假设3个商品，实际开发不可能这么传入，但问题不大，我们学的是设计模式不是业务。
 */
$goodsAName = $_POST['goodsAName'];
$goodsAPrice = $_POST['goodsAPrice'];
$goodsANum = $_POST['goodsANum'];

$goodsBName = $_POST['goodsBName'];
$goodsBPrice = $_POST['goodsBPrice'];
$goodsBNum = $_POST['goodsBNum'];

$goodsCName = $_POST['goodsCName'];
$goodsCPrice = $_POST['goodsCPrice'];
$goodsCNum = $_POST['goodsCNum'];

$totalA = $goodsAPrice * $goodsANum;
$totalB = $goodsBPrice * $goodsBNum;
$totalC = $goodsCPrice * $goodsCNum;
$total = $totalA + $totalB + $totalC;

$list[$goodsAName] = [
    'price' => $goodsAPrice,
    'num' => $goodsANum,
    'total' => $totalA,
];
$list[$goodsBName] = [
    'price' => $goodsBPrice,
    'num' => $goodsBNum,
    'total' => $totalB,
];
$list[$goodsCName] = [
    'price' => $goodsCPrice,
    'num' => $goodsCNum,
    'total' => $totalC,
];

foreach ($list as $goodsName => $data) {
    echo sprintf("%s：单价%d元,数量%d个,总价%d元\n", $goodsName, $data['price'], $data['num'], $data['total']);
}
echo '总价为：' . $total . '元';